<?php
class Trailors_Model extends Model {
	
	function __construct()
	{
		parent::Model();
	}
	
	function select()
	{
	$this->db->select('id,movie_name');
	$quary = $this->db->get('movies');
	 return $quary->result();
	}
	function rename($id,$details,$details1){
		$filepath=$details['upload_data']['file_path'];
		$filepath1=$details1['upload_data']['file_path'];
		if($filepath)
		{
			$oldname="assets/trailors/".$details['upload_data']['file_name'];
			rename($oldname,'assets/trailors/trailor'.$id.'.flv');
		}
		if($filepath1)
		{
			$oldname1="assets/trailors/preview_image/".$details1['upload_data']['file_name'];
			rename($oldname1,'assets/trailors/preview_image/trai_imge'.$id.'.jpg');
		}
	}
	
	// function to get trailors details 
	
	function get_trailors_by_movie($movie_id="")
	{
		$query = "SELECT count(trailors.t_id) as count FROM movies INNER JOIN trailors 
        ON (movies.id = trailors.movie_id) where trailors.movie_id= $movie_id";
        $result = $this->db->query($query);
        $count = $result->row();
 		return $count->count;
	}
	function get_trailor_fulldetails($id="",$limit="",$order='desc'){
			if($id!=""){
				$this->db->where("id",$id);
			}
			if($order!=""){
				$this->db->order_by("trailors.insert_date ",'desc');
			}
			if($limit!=""){
				$this->db->limit($limit);
			}
			$this->db->select('distinct(movies.id),movies.movie_name,trailors.movie_id,t_id,time_sec');
			$this->db->from('trailors');
			$this->db->join('movies','movies.id=trailors.movie_id');
			$details=$this->db->get_where();
			return $details->result();
		}
	function get_trailors($movie_id="",$limit=""){
		$this->db->select('*');
		$this->db->from('trailors');
		$this->db->join('movies','movies.id=trailors.movie_id');
		$array=array('movie_id'=>$movie_id);
		$this->db->order_by('trailors.t_id','desc');
		if($limit!="")
		$this->db->limit($limit);
		$details=$this->db->get_where('',$array);
		return $details->result();
	}
	function get_id($movie_id){
		$resulet=$this->db->get_where('trailors',array('movie_id'=>$movie_id));
		$deatils=$resulet->result();
		return $resulet->result();
	}
	function delete($trailor_id){
		$this->db->where('t_id',$trailor_id);
		$result=$this->db->delete('trailors');
		return $result;	
	}
	function getrecenttrailor()
	{
		$this->db->select_max('t_id');
		$trailor = $this->db->get('trailors');
		return $trailor->result_array();
	}
	function get_trailor_id(){
	$this->db->order_by('insert_date','desc');
	$query=$this->db->get('trailors');
	return $query->result();
	}
	
    function get_trailors_home($id="",$limit="",$order='desc'){

    	
				//$this->db->order_by('trailors.t_id','desc');
			
			if($limit!=""){
				$this->db->limit($limit);
			}
			$this->db->select('distinct(movies.id),movies.movie_name,trailors.movie_id,max(t_id) as t_id,time_sec');
			$this->db->from('trailors');
			$this->db->join('movies','movies.id=trailors.movie_id');
			$this->db->group_by('movies.id');
			$details=$this->db->get_where();
			//$res =$details->result();
			//print_r($res);exit;
			return $details->result();
			
//    $this->db->limit(5);
//	//$this->db->order_by('insert_date','desc');
//	$query=$this->db->get('trailors');
//	return $query->result();
	}

}
?>